package com.ruoyi.project.curricula.controller;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.curricula.domain.STCurriCula;
import com.ruoyi.project.curricula.service.STCurriCulaService;
import com.ruoyi.project.jiaowu.courseselcttime.domain.TbCourseSelectTime;
import com.ruoyi.project.jiaowu.courseselcttime.service.ITbCourseSelectTimeService;
import com.ruoyi.project.jiaowu.term.domain.TbTerm;
import com.ruoyi.project.jiaowu.term.service.ITbTermService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * 学生选课
 */
@Controller
@RequestMapping("website/curricula")
public class STCurriCulaController extends BaseController {

    private  String prefix="system/curricula";

    @Autowired
    private STCurriCulaService stCurriCulaService;

    @Autowired
    private ITbCourseSelectTimeService iTbCourseSelectTimeService;

    @Autowired
    private ITbTermService iTbTermService;

    @RequiresPermissions("website:curricula:view")
    @GetMapping()
    public String curricula(ModelMap map)
    {
        //获取学生类别
        Integer a=getSysUser().getStudentType();
        if(StringUtils.isNull(a) || a==null || a==0){
            TbCourseSelectTime tbCourseSelectTime=new TbCourseSelectTime();
            tbCourseSelectTime.setStartTime(new Date());
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
            c.add(Calendar.DAY_OF_MONTH, 1);
            tbCourseSelectTime.setEndTime(c.getTime());
            map.addAttribute("time",tbCourseSelectTime);
        }else{
            //查询学生对应的学生类型选课时间
            TbCourseSelectTime courseSelectTime=new TbCourseSelectTime();
            courseSelectTime.setStudentTypeId((long) a);
            TbCourseSelectTime tbCourseSelectTime=iTbCourseSelectTimeService.selectTbCourseSelectTimeList2(courseSelectTime);
            map.addAttribute("time",tbCourseSelectTime);
        }
        return prefix + "/curricula";
    }

    /**
     *查询开课计划列表
     */
    @RequiresPermissions("website:curricula:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectSTCurriCulaList(STCurriCula stCurriCula)
    {
        startPage();
        Long id=getSysUser().getUserId();
        stCurriCula.setUserId(id);
        Integer type=getSysUser().getStudentType();
        stCurriCula.setStudentType(type);
        //添加学生的专业
        stCurriCula.setProfession(getSysUser().getStudentProfession());
        //查询选课信息
        //List<STCurriCula> list = stCurriCulaService.selectSTCurriculaList(stCurriCula);
        Date date = new Date();
        List<TbTerm> tbTerms = iTbTermService.selectTbTermLists();
        Long stuYear = null;
        for(TbTerm tbTerm:tbTerms){
            if(tbTerm.getStartTime().getTime()<date.getTime() && tbTerm.getEndTime().getTime()>date.getTime()){
                stuYear = tbTerm.getId();
                break;
            }
        }
        stCurriCula.setSchoolYear(stuYear);
        List<STCurriCula> list = stCurriCulaService.selectSTCurriculaList2(stCurriCula);
        //System.out.println(list);
        for(STCurriCula lists:list){
            if(lists.getClassmaxweek()!=null && lists.getClasslowweek()!=null){
                lists.setClassWeek(lists.getClasslowweek().toString()+"-"+lists.getClassmaxweek().toString());
            }
        }
        return getDataTable(list);
    }


    @RequiresPermissions("website:curricula:xuan")
    @PostMapping("/xuan")
    @ResponseBody
    public AjaxResult STCurriCulaUpdate(Long id,int stuMaxNumber)
    {
        Long userId=getSysUser().getUserId();
        //查出已选人数 如果已选人数>课程人数就 不执行下面这个 返回选课人数已满
        int count= stCurriCulaService.result(id);
        int count1=stCurriCulaService.classStates(userId,id);
        if(count1>0){
            return  AjaxResult.error("已经选过课了");
        }
        if(count>=stuMaxNumber){
             return  AjaxResult.error("超出课程人数");
         }else {
            return toAjax( stCurriCulaService.insertCul(id,userId));
        }
    }



//    @Log(title = "取消选课", businessType = BusinessType.UPDATE)
//    @PostMapping("/curriCula")
//    @ResponseBody
//    public AjaxResult cleanCurriCula(Long id)
//    {
//        return toAjax(stCurriCulaService.cleanCurriCula(id));
//    }
//
    @Log(title = "取消选课", businessType = BusinessType.DELETE)
    @RequiresPermissions("website:curricula:xuanCurri")
    @PostMapping("/xuanCurri")
    @ResponseBody
    public AjaxResult xuanCurri(Long id ,Long userstates)
    {
        Long userId=getSysUser().getUserId();

        int count3=stCurriCulaService.xuanCurriCula(id, userId);
        return toAjax(stCurriCulaService.xuanCurriCula(id,userId));
    }

}
